<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/3
 * Time: 10:12
 */

namespace app\service;


use app\models\Customer;
use app\models\Inland;
use yii;

class InlandService
{
    public $inland;

    public function __construct()
    {
        $this->inland = new Inland();
    }


    public function updatePid()
    {
        $sql = 'UPDATE jingsourcing_inland_fee SET `pid`=`id` WHERE pid=0';
        return yii::$app->db->createCommand($sql)->execute();
    }

    public function checkMoneySame($pid)
    {
        $list = $this->inland->getListByPid($pid);

        $sum1 = $sum2 = 0;
        foreach ($list as $v) {
            if ($v['id'] == $v['pid']) {
                $sum1 += $v['total_fee'];
            } else {
                $sum2 += $v['total_fee'];
            }
        }

        if ($sum2 == 0 || round($sum1, 2) == round($sum2, 2)) {
            //金额校验一致
            $this->inland->save(['money_same' => 1], ['pid' => $pid]);
        } else {
            //金额不一致
            $this->inland->save(['money_same' => 2], ['pid' => $pid]);

        }
    }

    public function validateCustomer($list)
    {
        $arr = array_column($list, 'customer_num');
        $customermodal = new Customer();
        $arr = $customermodal->getCustomerByNum($arr);

        foreach ($list as $k => $v) {
            if (isset($arr[$v['customer_num']]) && $arr[$v['customer_num']] == $v['customer_name']) {
                $list[$k]['same'] = 1;
            } else {
                $list[$k]['same'] = 0;
            }
        }
        return $list;
    }

    public function list($p, $s, $where)
    {
        return $this->inland->get_inland_list($p, $s, $where);
    }

    public function size($where)
    {
        return $this->inland->get_inland_size($where);
    }
}